import path from 'node:path';
import process from 'node:process';

import { app, BrowserWindow } from 'electron';

process.env.APP_ROOT = path.join(import.meta.dirname, '../..');
export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist');
export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist');
// const width = process.env.TINY_DUST_WIN_WIDTH || 1100
const width = 1400;
const height = 800;
const devIndex =
  process.env.APP_MODE === 'development'
    ? `http://localhost:${process.env.APP_PORT}/`
    : undefined;
const indexHtml = `file://${path.join(RENDERER_DIST, 'index.html')}`;
process.env.VITE_PUBLIC = devIndex
  ? path.join(process.env.APP_ROOT, 'public')
  : RENDERER_DIST;
// const trayIcon = `file://${path.join(RENDERER_DIST, '64x64.png')}`;
const createWindow = () => {
  const win = new BrowserWindow({
    width,
    height,
    autoHideMenuBar: true,
    // webPreferences: {
    //   nodeIntegration: true,
    //   preload: path.join(import.meta.dirname, '../preload/index.mjs'),
    // },
  });
  const argvList = process.argv;
  let mainUrl = indexHtml;
  // const trayIconUrl = trayIcon;
  if (devIndex) {
    mainUrl = devIndex;
    // trayIconUrl = `${process.cwd()}/public/64x64.png`;
    win.webContents.openDevTools();
  }
  win.loadURL(mainUrl);
  if (argvList && argvList.length > 1 && argvList[1] === 'debug') {
    win.webContents.openDevTools();
  }
  // tray
  // const tray = new Tray(trayIconUrl);
  // tray.setContextMenu(
  //   Menu.buildFromTemplate([
  //     {
  //       label: '退出',
  //       click: () => {
  //         win.destroy();
  //         app.quit();
  //       },
  //     },
  //   ]),
  // );
};

app.whenReady().then(createWindow);
